//下载cnpm  install  axios  --save
import axios from "axios"
export default function ajax(url = "", params = {}, type = "get") {
    //1. 定义一个变量
    var promise
    return new Promise((resolve, reject) => {
        //2 判断请求的方式
        if (type == "get") {
            let paramsStr = ""
            Object.keys(params).forEach(key => {
                    paramsStr += key + "=" + params[key] + "&"
                })
                // console.log(paramsStr); //page=1&size=10&
            if (paramsStr != "") { //如果条件成立是说明有参数
                paramsStr = paramsStr.substr(0, paramsStr.lastIndexOf("&"))
            }
            url += "?" + paramsStr //接口地址 //http://localhost:3000/userlist?page=18
            promise = axios.get(url)

        } else if (type == "post") {
            promise = axios.post(url, params)
        }
        promise.then(response => {
            resolve(response.data) //参数.data数据成功
        }).catch(err => { //失败的时候
            reject(err)
        })
    })
}